<?php
namespace Admin\Model;
use Think\Model;
class WxmenuModel extends Model{
    protected $tableName = 'wxmenu';
    protected $pk        = 'id';

    public function getTree($parentid = 0){
        $field = array('id','upid','`name`','type','url','key','media_id','`id` as `operateid`');
        $order = '`sort` ASC,`id` ASC';
        $data = $this->field($field)->where(array('upid'=>$parentid))->order($order)->select();
        //echo($this->getLastSql());
        if (is_array($data)){
            foreach ($data as &$arr){
                $arr['children'] = $this->getTree($arr['id']);
            }
        }else{
            $data = array();
        }
        return $data;

    }
    public function getWxJson($parentid = 0){
        $field = array('`name`','type','url','key','media_id','id');
        $order = '`sort` ASC,`id` ASC';
        $data = $this->field($field)->where(array('upid'=>$parentid))->order($order)->select();
        if (is_array($data)){
            foreach ($data as &$sub_arr){
                $sub_arr['sub_button'] = $this->getWxJson($sub_arr['id']);
            }
        }else{
            $data = array();
        }
        //$arr['button']=$data;
        return $data;
        /*if($parentid==0){
            return json_encode($arr);
        }
        else{
            return $data;
        }*/

    }
    //栏目下拉列表
    public function getSelectTree($parentid = 0){
        $field = array('`id` as `id`','`name` as `text`');
        $order = '`sort` ASC,`id` ASC';
        $data = $this->field($field)->where(array('upid'=>$parentid))->order($order)->select();
        if (is_array($data)){
            foreach ($data as &$arr){
                $arr['children'] = $this->getSelectTree($arr['id']);
            }
        }else{
            $data = array();
        }
        return $data;
    }
}
?>